package cn.yllr.tomgxianghui.controller.admin;

import cn.yllr.tomgxianghui.entity.Dishes;
import cn.yllr.tomgxianghui.service.DishesService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class Admin_DishesController {
    @Resource
    DishesService dishesService;

    //菜品列表
    @RequestMapping("/viewDishes")
    public String viewDishes(Model model) {
        List<Dishes> dishes = dishesService.findAllDishes();
        model.addAttribute("dishes", dishes);
        return "admin/dish-list";
    }

    //增加菜品
    @RequestMapping("/toAddDishes")
    public String toAddDishes() {
        return "admin/dish-add";
    }

    @RequestMapping("/addDishes")
    public String addDishes(Dishes dishes) {
        dishesService.insert(dishes);
        return "redirect:/viewDishes";
    }

    //修改菜品
    @RequestMapping("/toUpdateDishes/{foodId}")
    public String toUpdateDishes(@PathVariable("foodId") Integer foodId, Model model) {
        Dishes dishes = dishesService.queryById(foodId);
        model.addAttribute("dishes", dishes);
        return "admin/dish-update";
    }

    @RequestMapping("/updateDishes")
    public String updateDishes(Dishes dishes) {
        dishesService.update(dishes);
        return "redirect:/viewDishes";
    }

    //删除菜品
    @RequestMapping("/deleteDishes/{foodId}")
    public String deleteDishes(@PathVariable("foodId") Integer foodId) {
        dishesService.deleteById(foodId);
        return "redirect:/viewDishes";
    }

    //搜索菜品
    @RequestMapping("/searchDishes/{foodId}")
    public String searchDishes(@PathVariable("foodId") Integer foodId, Model model) {
        Dishes dishes = dishesService.queryById(foodId);
        model.addAttribute("dishes", dishes);
        return "admin/search-dish";
    }
}
